/*********************************************************************************
 *      Copyright:  (C) 2020 no
 *                  All rights reserved.
 *
 *       Filename:  linked.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(2020年08月30日)
 *         Author:  wang ruijie <1046299465@qq.com>
 *      ChangeLog:  1, Release initial version on "2020年08月30日 19时31分51秒"
 *                 
 ********************************************************************************/
#include "linked.h"

/* 创建一个头节点来表示整个链表 */
struct Node *createlist()
{
    struct Node *headNode = (struct Node *)malloc(sizeof(struct Node)); //变成了一个结构体变量
    headNode->data = 1;
    headNode->next = NULL;

    return headNode;
}

/* 创建一个节点 */
struct Node *createNode(int data)
{
    struct Node *newlist = (struct Node *)malloc(sizeof(struct Node));
    newlist->data = data;
    newlist->next = NULL;

    return newlist;
}

/* 打印节点的数据 */
void listprint(struct Node *headNode)
{
    struct Node *pMove = headNode->next;
    
    while(pMove)
    {
        printf("%d", pMove->data);
        pMove = pMove->next;
    }
    printf("\n");
}

/* 插入节点 */
void insertNode(struct Node *headNode, int data)
{
    struct Node *newNode = createNode(data);

    newNode->next = headNode->next;
    headNode->next = newNode;
}

/* 删除节点 */
void deleteNode(struct Node *headNode, int Posdata)
{
    struct Node *posNode = headNode->next;
    struct Node *posNodeFront = headNode;

    if(posNode == NULL)
    {
        printf("无法删除\n");
    }
    else
    {
        while(posNode->data != Posdata)
        {
            posNodeFront = posNode;
            posNode = posNodeFront->next;

            if(posNode == NULL)
            {
                printf("没有找到该数据，无法删除\n");
                return ;
            }
        }
        posNodeFront->next = posNode->next;
        free(posNode);
    }
}

int main(int argc, char **argv)
{
    struct Node *list = createlist();
    insertNode(list, 1);
    insertNode(list, 2);
    insertNode(list, 3);

    listprint(list);

    deleteNode(list, 2);
    listprint(list);

    return 0;
}
